package com.example.coursehub.controller;

import com.example.coursehub.entity.Chapter;
import com.example.coursehub.entity.Course;
import com.example.coursehub.entity.Material;
import com.example.coursehub.service.StatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/statistics")
public class StatisticsController {
    private final StatisticsService statisticsService;
    
    @Autowired
    public StatisticsController(StatisticsService statisticsService) {
        this.statisticsService = statisticsService;
    }

    /**
     * 根据学生ID和课程ID获取课程的所有章节ID
     * @param studentId 学生ID
     * @param courseId 课程ID
     * @return 课程的所有章节ID列表
     */
    @GetMapping("/chapters/student/{studentId}/course/{courseId}")
    public List<String> getChapterIdsByStudentAndCourse(@PathVariable String studentId, @PathVariable String courseId) {
        return statisticsService.getChapterIdsByStudentAndCourse(studentId, courseId);
    }

    /**
     * 根据章节ID获取章节下的所有材料ID
     * @param chapterId 章节ID
     * @return 章节下的所有材料ID列表
     */
    @GetMapping("/materials/chapter/{chapterId}")
    public List<String> getMaterialIdsByChapter(@PathVariable String chapterId) {
        return statisticsService.getMaterialIdsByChapter(chapterId);
    }

    /**
     * 根据章节ID获取章节实体类
     * @param chapterId 章节ID
     * @return 章节实体类
     */
    @GetMapping("/chapters/{chapterId}")
    public Chapter getChapterById(@PathVariable String chapterId) {
        return statisticsService.getChapterById(chapterId);
    }

    /**
     * 根据课程ID获取课程实体类
     * @param courseId 课程ID
     * @return 课程实体类
     */
    @GetMapping("/courses/{courseId}")
    public Course getCourseById(@PathVariable String courseId) {
        return statisticsService.getCourseById(courseId);
    }

    /**
     * 根据资料ID获取资料实体类
     * @param materialId 资料ID
     * @return 资料实体类
     */
    @GetMapping("/materials/{materialId}")
    public Material getMaterialById(@PathVariable String materialId) {
        return statisticsService.getMaterialById(materialId);
    }

    /**
     * 根据课程ID获取课程的所有章节ID
     * @param courseId 课程ID
     * @return 课程的所有章节ID列表
     */
    @GetMapping("/chapters/course/{courseId}")
    public List<String> getChapterIdsByCourse(@PathVariable String courseId) {
        return statisticsService.getChapterIdsByCourse(courseId);
    }

    /**
     * 根据课程ID和学生ID获取学生的课程学习进度
     * @param courseId 课程ID
     * @param studentId 学生ID
     * @return 学生的课程学习进度百分比
     */
    @GetMapping("/progress/course/{courseId}/student/{studentId}")
    public int getCourseProgress(@PathVariable String courseId, @PathVariable String studentId) {
        return statisticsService.getCourseProgress(courseId, studentId);
    }

    /**
     * 根据章节ID和学生ID获取学生的章节学习进度
     * @param chapterId 章节ID
     * @param studentId 学生ID
     * @return 学生的章节学习进度百分比
     */
    @GetMapping("/progress/chapter/{chapterId}/student/{studentId}")
    public int getChapterProgress(@PathVariable String chapterId, @PathVariable String studentId) {
        return statisticsService.getChapterProgress(chapterId, studentId);
    }

    /**
     * 根据资料ID和学生ID获取学生的资料学习进度
     * @param materialId 资料ID
     * @param studentId 学生ID
     * @return 学生的资料学习进度百分比
     */
    @GetMapping("/progress/material/{materialId}/student/{studentId}")
    public int getMaterialProgress(@PathVariable String materialId, @PathVariable String studentId) {
        return statisticsService.getMaterialProgress(materialId, studentId);
    }

    /**
     * 根据班级ID计算班级的平均课程完成率
     * 完成率计算公式：
     * (所有学生完成的章节总数) / (课程章节总数 × 学生总数)
     *
     * @param classId 班级ID
     * @return 包含完成率的JSON对象 { "completionRate": 0.75 }
     */
    @GetMapping("/class-completion-rate/{classId}")
    public ResponseEntity<?> getClassCompletionRate(@PathVariable String classId) {
        try {
            int completionRate = statisticsService.calculateClassCompletionRate(classId);
            return ResponseEntity.ok(Map.of("completionRate", completionRate));
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("计算班级完成率时发生错误");
        }
    }

    /**
     * 获取作业完成率（学生提交比例）
     *
     * @param exerciseId 作业ID
     * @return 包含完成率的JSON对象 { "completionRate": 75 }
     */
    @GetMapping("/exercise-completion-rate/{exerciseId}")
    public ResponseEntity<?> getExerciseCompletionRate(@PathVariable String exerciseId) {
        try {
            int completionRate = statisticsService.calculateExerciseCompletionRate(exerciseId);
            return ResponseEntity.ok(Map.of("completionRate", completionRate));
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("计算作业完成率时发生错误");
        }
    }

}